/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package _bai02_phantichphankhaibao;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * VM i TH {1 .. n-1}
 * @author Administrator
 */
public class jClass_PhanKhaiBaoBieuThucLuongTu implements Interface_CacThanhPhanDacTa {

    private jEnum_BieuThucLuongTu _LoaiBieuThuc;
    private String _TenBienDiem;
    private String _ViTriDemDau;
    private String _ViTriDemCuoi;
    //private String _BuocNhay;
    //private jClass_TruongHopDacTa _DieuKien;
    private static String _BieuThucDinhDang;
    /**
     * @return the _BieuThucDinhDang
     */
    public static String getBieuThucDinhDang() {
        return _BieuThucDinhDang;
    }

    /**
     * @param aBieuThucDinhDang the _BieuThucDinhDang to set
     */
    public static void setBieuThucDinhDang(String aBieuThucDinhDang) {
        _BieuThucDinhDang = aBieuThucDinhDang;
    }

    /**
     * @return the _LoaiBieuThuc
     */
    public jEnum_BieuThucLuongTu getLoaiBieuThuc() {
        return _LoaiBieuThuc;
    }

    /**
     * @param loaiBieuThuc the _LoaiBieuThuc to set
     */
    public void setLoaiBieuThuc(jEnum_BieuThucLuongTu loaiBieuThuc) {
        this._LoaiBieuThuc = loaiBieuThuc;
    }

    /**
     * @return the _TenBienDiem
     */
    public String getBienDiem() {
        return getTenBienDiem();
    }

    /**
     * @param BienDiem the _TenBienDiem to set
     */
    public void setBienDiem(String BienDiem) {
        this.setTenBienDiem(BienDiem);
    }

    /**
     * @return the _ViTriDemDau
     */
    public String getViTriDiemDau() {
        return _ViTriDemDau;
    }

    /**
     * @param ViTriDiemDau the _ViTriDemDau to set
     */
    public void setViTriDiemDau(String ViTriDiemDau) {
        this._ViTriDemDau = ViTriDiemDau;
    }

    /**
     * @return the _ViTriDemCuoi
     */
    public String getViTriDemCuoi() {
        return _ViTriDemCuoi;
    }

    /**
     * @param ViTriDemCuoi the _ViTriDemCuoi to set
     */
    public void setViTriDemCuoi(String ViTriDemCuoi) {
        this._ViTriDemCuoi = ViTriDemCuoi;
    }

    public jClass_PhanKhaiBaoBieuThucLuongTu(){
        _LoaiBieuThuc = jEnum_BieuThucLuongTu.TonTai;
        _TenBienDiem = "i";
        //_DieuKien = new jClass_TruongHopDacTa();
        //_BuocNhay = "1";
        _ViTriDemCuoi = "0";
        _ViTriDemDau = "n - 1";
        _BieuThucDinhDang = "(";
        for (jEnum_BieuThucLuongTu bieuThucLuongTu : jEnum_BieuThucLuongTu.values())
            _BieuThucDinhDang += bieuThucLuongTu.value() + "|";
        _BieuThucDinhDang = _BieuThucDinhDang.substring(0, _BieuThucDinhDang.lastIndexOf("|")) + ")";
        //(VM|TT)
        _BieuThucDinhDang += ".+" + jEnum_CacKyTuDacBietCoTheChoNguoiDungDinhNghia.Thuoc.value();
        //(VM|TT) i TH
        _BieuThucDinhDang += "\\s*\\{.+\\.\\..+\\}";
        //VM i TH {1 .. n-1}
    }

    public void jCapNhatTuChuoi(String Str_KhaiBao) {
        //throw new UnsupportedOperationException("Not supported yet.");
        Str_KhaiBao = Str_KhaiBao.trim();
        String tachCacPhan[] = Str_KhaiBao.split(" ", 3);
        for (jEnum_BieuThucLuongTu bieuThucLuongTu : jEnum_BieuThucLuongTu.values())
            if (bieuThucLuongTu.value().equals(tachCacPhan[0]))
                _LoaiBieuThuc = bieuThucLuongTu;
        if (!jClass_TenBien.jKiemTraChuoiHopDinhDang(tachCacPhan[1])){
            //Doan thu 2 khong phai la mot ten bien
            //Bao loi va exc
        }
        _TenBienDiem = tachCacPhan[1].trim();
        String TH = jEnum_CacKyTuDacBietCoTheChoNguoiDungDinhNghia.Thuoc.value();
        if (!tachCacPhan[2].startsWith(TH)){
            //Doan thu 3 khong bat dau = TH
            //Bao loi va exc
        }
        String haiDauDuyet[] = tachCacPhan[2].substring(TH.length()).trim().split("\\.\\.", 2);
        _ViTriDemDau = haiDauDuyet[0].substring(1, haiDauDuyet[0].length()).trim();
        if (!jClass_GiaTri.jKiemTraChuoiHopDinhDang(_ViTriDemDau)){

        }

        _ViTriDemCuoi = haiDauDuyet[1].substring(0, haiDauDuyet[1].length() - 1).trim();
        if (!jClass_GiaTri.jKiemTraChuoiHopDinhDang(_ViTriDemDau)){
            
        }
    }

    public Boolean jKiemTraChuoiHopDinhDang(String str_ChuoiCanKienTra) {
        Pattern pat=Pattern.compile(getBieuThucDinhDang());
        Matcher matcher =pat.matcher(str_ChuoiCanKienTra);
        return matcher.matches();
    }

    public String jToCCode(int DoLui) {
        //throw new UnsupportedOperationException("Not supported yet.");
        String Kq = "for (int " + _TenBienDiem + " = " + _ViTriDemDau + "; ";
        Kq += _TenBienDiem + " <= " + _ViTriDemCuoi + " && ";
        Kq +="; " + _TenBienDiem + "++";
        Kq += ")";
        return Kq;
    }

    public String jCSharpCode(int DoLui) {
        //throw new UnsupportedOperationException("Not supported yet.");
        String Kq = "for (int " + _TenBienDiem + " = " + _ViTriDemDau + "; ";
        Kq += _TenBienDiem + " <= " + _ViTriDemCuoi + " && ";
        Kq +="; " + _TenBienDiem + "++";
        Kq += ")";
        return Kq;
    }

    public String jVBNetCode(int DoLui) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @return the _TenBienDiem
     */
    public String getTenBienDiem() {
        return _TenBienDiem;
    }

    /**
     * @param TenBienDiem the _TenBienDiem to set
     */
    public void setTenBienDiem(String TenBienDiem) {
        this._TenBienDiem = TenBienDiem;
    }

    public String jToCCode(int DoLui, String bienKq) {
        //throw new UnsupportedOperationException("Not supported yet.");
        String Kq = "for (int " + _TenBienDiem + " = " + _ViTriDemDau + "; ";
        Kq += _TenBienDiem + " <= " + _ViTriDemCuoi + " && ";
        if (_LoaiBieuThuc == jEnum_BieuThucLuongTu.TonTai)
            Kq+="!";
        Kq+= bienKq;
        Kq +="; " + _TenBienDiem + "++";
        Kq += ")";
        return Kq;
    }
     public String jToCSharpCode(int DoLui, String bienKq) {
        //throw new UnsupportedOperationException("Not supported yet.");
        String Kq = "for (int " + _TenBienDiem + " = " + _ViTriDemDau + "; ";
        Kq += _TenBienDiem + " <= " + _ViTriDemCuoi + " && ";
        if (_LoaiBieuThuc == jEnum_BieuThucLuongTu.TonTai)
            Kq+="!";
        Kq+= bienKq;
        Kq +="; " + _TenBienDiem + "++";
        Kq += ")";
        return Kq;
    }
}
